package com.yangjie.web.jsf.syslog;

import javax.faces.context.FacesContext;
import javax.inject.Inject;

import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.web.context.WebApplicationContext;

import com.yangjie.common.dao.criteria.CriteriaExpressionIF;
import com.yangjie.common.dao.criteria.PaginationQueryCriteria;
import com.yangjie.common.dao.criteria.QueryCriteria;
import com.yangjie.common.dao.criteria.UnaryExpression;
import com.yangjie.common.syslog.SysLog;
import com.yangjie.common.syslog.SysLogService;
import com.yangjie.user.domain.User;
import com.yangjie.web.jsf.user.UserDataModel;

@Component
//@Scope(value = WebApplicationContext.SCOPE_REQUEST)
@Scope("jsfView")
public class SysLogBean {
	Logger logger = Logger.getLogger(this.getClass());
	
	@Inject
	private SysLogService sysLogService = null;
	
	LazySysLogDataModel logDataModel = null;
	private SysLog[] selectedLogs = null;
	private SysLog log = null;	
	private UnaryExpression userFilter = new UnaryExpression("userName", CriteriaExpressionIF.OP_EQUAL);
    private PaginationQueryCriteria criteria = new PaginationQueryCriteria();
	
    private int id = -1;
    
	public int getId() {
		return id;
	}


	public void setId(int id) {
		this.id = id;
	}


	public SysLog getLog() {
		return log;
	}


	public void setLog(SysLog log) {
		this.log = log;
	}


	public UnaryExpression getUserFilter() {
		return userFilter;
	}


	public void setUserFilter(UnaryExpression userFilter) {
		this.userFilter = userFilter;
	}


	public SysLog[] getSelectedLogs() {
		return selectedLogs;
	}


	public void setSelectedLogs(SysLog[] selectedLogs) {
		this.selectedLogs = selectedLogs;
	}

	public void delete() {
		logger.debug("delete selected log ...");
		try {
			for(SysLog log: selectedLogs) {
				sysLogService.delete(log);
			}
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
	

	public LazySysLogDataModel getLogDataModel() {
		if(logDataModel == null) {
			logger.debug("logDataModel is null...");
			logDataModel = new LazySysLogDataModel();
			logDataModel.setSysLogService(sysLogService);
//			FacesContext facesContext = FacesContext.getCurrentInstance();
//			QueryCriteria savedCriteria = (QueryCriteria)facesContext.getExternalContext().getSessionMap().get("sysLogCriteria");			
//			if(savedCriteria != null) {
//				criteria.setCriteria(savedCriteria);
//			}
			logDataModel.setCriteria(criteria);
		}
		logger.debug("logDataModel is not null...");
		return logDataModel;
	}
	
	public LazySysLogDataModel initLazySysLogDataModel() {
		LazySysLogDataModel retval = new LazySysLogDataModel();
		PaginationQueryCriteria criteria = new PaginationQueryCriteria();
		retval.setCriteria(criteria);
		retval.setSysLogService(sysLogService);
		return retval;
		
	}
	public void test() {
		logger.debug("test");
	}
	public void deleteSingle() {
		logger.debug("delete log ...");
		sysLogService.delete(log);
	}
	
	public void deleteById(int id) {
		logger.debug("delete log by id...");
		sysLogService.deleteById(id);
	}
	
	public void query() {
		logger.debug("query.....");
		criteria.clear();		
		criteria.getCriteria().addExpr(userFilter);

//		FacesContext facesContext = FacesContext.getCurrentInstance();
//		facesContext.getExternalContext().getSessionMap().put("sysLogCriteria", criteria.getCriteria());
	}
}
